package com.xyht.sca_s.student_manage_system.modules.infrastructureProject.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.infrastructureProject.entity.dto.SmsInfrastructureProjectAllowDto;
import com.xyht.sca_s.student_manage_system.modules.infrastructureProject.entity.dto.SmsInfrastructureProjectCancelDto;
import com.xyht.sca_s.student_manage_system.modules.infrastructureProject.entity.dto.SmsInfrastructureProjectDto;
import com.xyht.sca_s.student_manage_system.modules.infrastructureProject.entity.dto.SmsInfrastructureProjectHistoryReq;
import com.xyht.sca_s.student_manage_system.modules.infrastructureProject.service.SmsInfrastructureProjectApprovalService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 * 基础建设审批表 前端控制器
 * </p>tu
 *
 * @author XYHT
 * @since 2023-05-23
 */
@RestController
@RequestMapping("/infrastructureProject")
@SecretBody
public class SmsInfrastructureProjectApprovalController {

    @Resource
    private SmsInfrastructureProjectApprovalService  infrastructureProjectService;

    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty("提交审批")
    @PostMapping("/add")
    public ResponseResult addInfrastructureApproval(HttpServletRequest request, @RequestBody @Validated SmsInfrastructureProjectDto infrastructureProjectDto){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return infrastructureProjectService.addInfrastructureApproval(userId,infrastructureProjectDto);
    }

    @ApiModelProperty("撤销申请")
    @PostMapping("/cancel")
    public ResponseResult cancelInfrastructureApproval(HttpServletRequest request, @RequestBody SmsInfrastructureProjectCancelDto cancelDto){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return infrastructureProjectService.cancelInfrastructureApproval(userId,cancelDto);
    }

    @ApiModelProperty("审批申请")
    @PostMapping("/allow")
    public ResponseResult allowInfrastructureApproval(HttpServletRequest request, @RequestBody
                                                      SmsInfrastructureProjectAllowDto allowDto){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return infrastructureProjectService.allowInfrastructureApproval(userId,allowDto);
    }

    @ApiModelProperty("申请人指定流程(申请前获取)")
    @GetMapping("/processList")
    public ResponseResult getInfrastructureOriginSettingProcess(){
        return infrastructureProjectService.getInfrastructureOriginSettingProcess();
    }

    @ApiModelProperty("获取审批详情")
    @GetMapping("/detail")
    public ResponseResult getInfrastructureDetail(HttpServletRequest request,
                                                  @RequestParam(value = "examineId",required = false)String examineId){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return infrastructureProjectService.getInfrastructureDetail(userId,examineId);
    }

    @ApiModelProperty("获取审批列表")
    @GetMapping("/list")
    public ResponseResult getInfrastructureList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                @RequestParam(value = "examine_type",required = false) Integer examine_type,
                                                @RequestParam(value = "search_str",required = false) String search_str,
                                                HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return infrastructureProjectService.getInfrastructureList(userId,examine_type,pageNum,pageSize,search_str);
    }

    @ApiModelProperty(value = "申请表下载")
    @GetMapping("/download")
    public ResponseResult tableDownload(@RequestParam(value = "apply_id") String apply_id, HttpServletResponse response) {
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = infrastructureProjectService.tableDownload(apply_id, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiModelProperty("查看所有记录")
    @GetMapping("/allList")
    public ResponseResult getAllInfrastructureList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value = "allowStatus",required = false) Integer allowStatus,
                                                   @RequestParam(value = "applyName",required = false) String applyName,
                                                   @RequestParam(value = "applyDepartment",required = false) String applyDepartment){
        return infrastructureProjectService.getAllInfrastructureList(allowStatus,applyName,applyDepartment,pageSize,pageNum);
    }

    @ApiModelProperty(value = "导出历史记录")
    @PostMapping("/exportHistory")
    public ResponseResult exportExamineHistoryList(@RequestBody SmsInfrastructureProjectHistoryReq req, HttpServletResponse response) {
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = infrastructureProjectService.exportExamineHistoryList(req, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

}

